<?php

namespace App\Admin\Controllers;

use App\Models\Grade;
use App\Models\Major;
use App\Models\Teacher;
use App\Models\Student;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;


class GradeController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('班级管理')
            ->description('班级信息')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('班级管理')
            ->description('查看班级')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('班级管理')
            ->description('修改班级')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('班级管理')
            ->description('新建班级')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Grade);

        $grid->model()->orderBy('id','desc');
        $grid->id('Id')->sortable();
        $grid->major_id('所属专业')->display(function ($major_id) {
            $major = Major::where('id', $major_id)->first();
            return $major->title;
        });
        $grid->class_code('班级编码');
        $grid->class_name('班级名称');
        $grid->class_teacher('班主任')->display(function ($class_teacher) {

            if($class_teacher == 0) {
                return '待定';
            }else {
                $teacher = Teacher::where('id', $class_teacher)->first();
                return $teacher->name;
            }
        });
        $grid->column('班级人数')->display(function () {
            $counts = Student::where('class_id', $this->id)->where('id','>',0)->count();
            return $counts;
        });
        $grid->created_at('创建日期');
        $grid->updated_at('修改日期');
        $grid->actions(function ($actions) {
//            $actions->disableDelete();
//            $actions->disableEdit();
            $actions->disableView();
        });

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->column(1/2, function ($filter) {
                $filter->like('class_code', '班级编码');
                $filter->like('class_name', '班级名称');
            });

            $filter->column(1/2, function ($filter) {
                $majors = DB::table('major')->where('parent_id', '<>', 0)->get();
                $mafor_arr = [];
                foreach ($majors as $major) {
                    $mafor_arr[$major->id] = $major->title;
                }
                $teachers = Teacher::where('on_duty','=', 0)->get();
                $teacher_arr = [0 => '待定'];
                foreach ($teachers as $teacher) {
                    $teacher_arr[$teacher->id] = $teacher->name;
                }

                $filter->equal('major_id', '所属专业')->select($mafor_arr);

                $filter->equal('class_teacher','班主任')->select($teacher_arr);

            });
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Grade::findOrFail($id));

//        $show->name('学期名称');
//        $show->start_date('开始日期');
//        $show->end_date('结束日期');
//        $show->is_semester('Is semester');
//        $show->created_at('Created at');
//        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Grade);

        $major = new Major();

        $teachers = Teacher::where('on_duty','=', 0)->get();
        $teacher_arr = [0 => '待定'];

        foreach ($teachers as $teacher) {
            $teacher_arr[$teacher->id] = $teacher->name;
        }

        $form->select('major_id', trans('所属专业'))->options($major::selectOptions())->setWidth(3, 2);
        $form->text('class_code', '班级代码')->setWidth(3, 2)->rules('required', [
            'required' => '班级代码不能为空'
        ]);;
        $form->text('class_name', '班级名称')->setWidth(3, 2)->rules('required', [
            'required' => '班级名称不能为空'
        ]);;
        $form->select('class_teacher', trans('班主任'))->options($teacher_arr)->setWidth(3, 2);

        $form->footer(function ($footer) {
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });

        //判断新建数据还是修改数据，新建数据对学期名称进行校验
        $form->saving(function (Form $form) {
            $id = $form->model()->id;

            //判断是否有id
            if(!$id){
                $request = request()->all();

                $major_id = $request['major_id'];
                $major_pid = 0;
                if($major_id != '0') {
                    $major = Major::where('id', $major_id)->first();
                    $major_pid = $major->parent_id;
                }

                if($major_pid == '0' && $major_pid == 0) {
                    $title = '提示';
                    $message = '选择具体专业';
                }

                $class_code = $request['class_code'];
                $grade = Grade::where('class_code', $class_code)->first();
                if(!empty($grade) ){
                    $title = '提示';
                    $message = '班级编码已存在';
                }

                if(!empty($title)){
                    $error = new MessageBag([
                        'title'   => $title,
                        'message' => $message,
                    ]);
                    return back()->with(compact('error'))->withInput();
                }
            }
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });


        return $form;
    }
}
